package com.zy.fee.roadpark.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.config.Urls;
import com.zy.fee.roadpark.model.apimodel.APIM_MessageList;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;

/**
 * Created by Administrator on 2018/9/13 0013.
 */

public class MessageLogic {
  /**
   * 消息列表
   */
  public static void nbnode_message_list(final Context mContext, String adminID, int page, int rows,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_MESSAGE_LIST, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("page", page);
    request.add("rows", rows);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_MessageList result = JsonUtil.jsonToObject(response.get(), APIM_MessageList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 设置管理员消息为已读
   */
  public static void nbnode_message_doRead(final Context mContext, String adminMessageID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_MESSAGE_DOREAD, RequestMethod.POST);
    request.add("adminMessageID", adminMessageID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
